package com.pan.cuiniubi.create.dao;

import com.pan.cuiniubi.entity.*;
import com.pan.cuiniubi.enumn.LaminaEnum;
import com.pan.cuiniubi.enumn.OperationEnum;
import com.pan.cuiniubi.utils.ReadYamlTool;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CreateSelectDaoMapping extends CreateDaoMappingImpl {

    public String getClassName(String name){
        return super.getClassName(name,OperationEnum.select);
    }

    @Override
    public DaoInfo getDaoInfo(Table table){

        DaoInfo daoInfo = new DaoInfo();
        daoInfo.setImportStr(getImportString(table))
                .setPackageStr(getPackageStr())
                .setMethodInfoList(getMethodInfo(table))
                .setClassName(getClassName(table.getTableName(), OperationEnum.select))
                .setEntityName(ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity,"path")+"."+table.getTableName());
        return daoInfo;
    }


    @Override
    public List<MethodInfo> getMethodInfo(Table table) {
        List<Member> memberList = table.getMemberList();
        String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity, "path");
        String entityName = path+"."+table.getTableName();
        String returnListStr = "java.util.List<"+entityName+">";
        String returnStr = entityName;

        List<MethodInfo> methodInfoList = new ArrayList<>();
        for (Member member : memberList){

            String rStr = returnListStr;
            if (member.getIsPrimary()){
                rStr = returnStr;
            }
            String memberName = member.getMemberName();
            String methodName = "selectBy"+memberName.substring(0,1).toUpperCase()+memberName.substring(1);
            MethodInfo methodInfo = new MethodInfo();
            methodInfo.setMethodName(methodName)
                    .setParameterList(member)
                    .setReturnStr(rStr);
            methodInfoList.add(methodInfo);
        }
        return methodInfoList;
    }

    public List<SqlInfo> getSqlInfo(Table table){
        String tableName =  table.getTableName();
        String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity,"path");
        List<Member> memberList = table.getMemberList();
        List<SqlInfo> sqlInfoList = memberList.stream().map(member -> {
            SqlInfo sqlInfo = new SqlInfo();
            sqlInfo.setOpt(OperationEnum.select.name());
            sqlInfo.setResult(path+"."+tableName);
            sqlInfo.setId("selectBy"+member.getMemberName().substring(0,1).toUpperCase()
                    +member.getMemberName().substring(1));
            sqlInfo.setSql("select * from "+tableName+" where "+member.getMemberName()+"=#{"+member.getMemberName()+"};");
            return sqlInfo;
        }).collect(Collectors.toList());
        return sqlInfoList;
    }

    public MappingInfo getMappingInfo(Table table){
        MappingInfo mappingInfo = new MappingInfo();
        mappingInfo.setNamespace(getNamespace(table.getTableName(),OperationEnum.select));
        mappingInfo.setSqlInfoList(getSqlInfo(table));
        String className = getClassName(table.getTableName(),OperationEnum.select);
        mappingInfo.setClassName(className);
        return mappingInfo;
    }

}
